import * as fs from 'fs';
import * as path from 'path';
import * as electron from 'electron'

// ES6 Module
import * as menu from './menu';
import { editorHistory, TestCommand, AbstractCommand } from './history';
import { AbstractPropertyItemRenderer, createPropertyItemRenderer } from './propertyitemrenderer';
import { project } from './project';
import { changeDataInspector, getInspector, DataInspector } from './dataInspector';
import { initPreviewBoard, openPreviewDebugPanel, openMapEditor, openPreviewPanel } from './mainboard';
import { currentData } from './currentdata';





menu.run();

initPreviewBoard();

export interface PropertyMetadata {

    key: string,
    description: string,
    type: "string" | 'number' | "dropdown" | "primarykey"|"image",
    default: any;
    options?: {
        filename: string,
        prefix: string
    }
}



export class ModifyPropertyCommand extends AbstractCommand{
    constructor(private obj: any, private to: any, private from: any, private key: string, private inspector: DataInspector, private renderer:AbstractPropertyItemRenderer) {
        super();
    }

    execute(): void {
        this.obj[this.key] = this.to;
        this.inspector.updateItem(this.obj);
    }
    revert(): void {
        this.obj[this.key] = this.from;
        this.renderer.updateView(this.from);
    }
}



export interface DataMetadata {
    filename: string;
    prefix: string;
    title: string;
    propertyMetadatas: PropertyMetadata[]
}

const missionMetadata: DataMetadata = {
    filename: "mission.json",
    title: "任务编辑器",
    prefix: "tasks",
    propertyMetadatas: [
        {
            key: 'id',
            description: "ID",
            default: "0",
            type: "primarykey"
        },
        {
            key: 'title',
            description: "任务标题",
            default: "新的任务",
            type: "string"
        },
        {
            key: 'needLevel',
            description: "接受任务需要的等级",
            default: 1,
            type: "number"
        },
        {
            key: 'fromNpcId',
            description: "接受任务的NPCID",
            default: 1,
            type: "dropdown",
            options: {
                filename: "npc.json",
                prefix: "npc"

            }
        },
        {
            key: 'toNpcId',
            description: "提交任务的NPCID",
            default: 1,
            type: "dropdown",
            options: {
                filename:'npc.json',
                prefix: "npc"

            }
        }
        // {
        //     key: 'canAcceptContent',
        //     description: "可接受任务npc话语：",
        //     default: "你好",
        //     type:"string"
        // },
        // {
        //     key: 'canSubmitContent',
        //     description: "可提交任务npc话语：",
        //     default: "再见 ",
        //     type:"string"
        // }
    ]
}

const AcceptStatementsMatadata:DataMetadata={
    filename:"mission.json",
    prefix:'canAcceptContent',
    title:"接受任务对话编辑",
    propertyMetadatas:[
       {
           key:'id',
           description:'匹配任务ID',
           type:'number',
           default:1,
       },
       {
           key:'order',
           description:'对话顺序',
           type:'number',
           default:0
       },
       {
        key:'name',
        description:'说话人的名字',
        type:'string',
        default:'你猜'
    },
    {
        key:'statements',
        description:'接收任务话语',
        type:'string',
        default:'你好'
    },
    {
        key:'Image',
        description:'说话者头像',
        type:'image',
        default:''
    },

    ]
}

const SubmitStatementsMatadata:DataMetadata={
    filename:"mission.json",
    prefix:'canSubmitContent',
    title:"提交任务对话编辑",
    propertyMetadatas:[
       {
           key:'id',
           description:'匹配任务ID',
           type:'number',
           default:1,
       },
       {
           key:'order',
           description:'对话顺序',
           type:'number',
           default:0
       },
       {
        key:'name',
        description:'说话人的名字',
        type:'string',
        default:'你猜'
    },
    {
        key:'statements',
        description:'接收任务话语',
        type:'string',
        default:'你好'
    },
    {
        key:'Image',
        description:'说话者头像',
        type:'image',
        default:''
    },

    ]
}

const npcMetadata: DataMetadata = {
    filename: "npc.json",
    prefix: 'npc',
    title: "NPC编辑器",
    propertyMetadatas: [
        {
            key: 'mapId',
            description: "NPC所在地图ID",
            default: "0",
            type:'number'
        },
        {
            key: 'id',
            description: "ID",
            default: "0",
            type: "primarykey"
        },
        {
            key: 'name',
            description: "NPC姓名",
            type: "string",
            default: "无名氏"
        },
        {
            key: 'image',
            description: "NPC头像",
            type: "image",
            default: ""
        },
        {
            key: "avatar",
            description: "NPC模型",
            type: "image",
            default: ""
        },
        {
            key: 'x',
            description: 'npc的位置_x',
            type: 'number',
            default:0
        },
        {
            key: 'y',
            description: 'npc的位置_y',
            type: 'number',
            default:0
        }
    ]
}
const itemMetadata: DataMetadata = {
    filename: "item.json",
    prefix: 'item',
    title: '道具编辑',
    propertyMetadatas: [
        {
            key: 'mapId',
            description: '所在地图id',
            type: 'number',
            default:1
        },
        {
            key: 'name',
            description: '道具名字',
            type: 'string',
            default:'这是个道具'
        },
        {
            key: 'x',
            description: '道具的位置_X',
            type: 'number',
            default:0
        },
        {
            key: 'y',
            description: '道具的位置_ Y',
            type: 'number',
            default:0
        },
        {
            key: 'image',
            description: "道具图片",
            type: 'image',
            default:""
        },
        {
            key:'avatar',
            description:'背包内图片选择',
            type:'image',
            default:""
        },
        {
            key:'avatarX',
            description:'背包道具位置X',
            type:"number",
            default:300
            
        },
        {
            key:'avatarY',
            description:'背包道具位置Y',
            type:"number",
            default:300
            
        },
        {
            key:'description',
            description:"道具描述",
            type:"string",
            default:"这是道具的描述"
        },
        {
            key :'id',
            description:'道具id编辑',
            type:'number',
            default:'0'
        }

    ]
}

const monnsterMetadata:DataMetadata={
    filename:"monster.json",
    prefix:"monster",
    title:"怪物编辑",
    propertyMetadatas:[
        {
            key:'mapId',
            description:'匹配地图ID',
            type:"number",
            default:'0'
        },
        {
            key:'attack',
            description:'怪物攻击',
            type:'number',
            default:'20'

        },
        {
            key:'hp',
            description:'怪物血量',
            type:'number',
            default:'50'
        },
        {
            key:'level',
            description:'怪物等级',
            type:'number',
            default:'2'
        },
        {
            key:'id',
            description:"怪物id",
            type:'number',
            default:'0'
        },
        {
            key:'name',
            description:"怪物名字",
            type:'string',
            default:'怪物'
        },
        {
            key:'image',
            description:"怪物图片",
            type:'image',
            default:''
        },
        {
            key:'x',
            description:'怪物位置X',
            type:"number",
            default:'0'
        },
        {
            key:'y',
            description:'怪物位置Y',
            type:'number',
            default:'0'
        }
    ]
}

const map1Metadata: DataMetadata = {
    filename: "map.json",
    prefix: 'map',
    title: "地图1编辑",
    propertyMetadatas: [
        
        {
            key: 'transmissionX',
            description: '传送位置X',
            type: 'number',
            default:4
        },
        {
            key: 'transmissionY',
            description: '传送位置Y',
            type: 'number',
            default:4
        },
        {
            key:'music',
            description:'音乐地址',
            type:'string',
            default:''  
          }
        
    ]
}
const map2Metadata: DataMetadata = {
    filename: "map2.json",
    prefix: 'map',
    title: "地图2编辑",
    propertyMetadatas: [
        {
            key: 'transmissionX',
            description: '传送位置X',
            type: 'number',
            default:4
        },
        {
            key: 'transmissionY',
            description: '传送位置Y',
            type: 'number',
            default:4
        },
        {
            key:'music',
            description:'音乐地址',
            type:'string',
            default:''  
          }
    ]
}
const map3Metadata: DataMetadata = {
    filename: "map3.json",
    prefix: 'map',
    title: "地图3编辑",
    propertyMetadatas: [
        
        {
            key: 'transmissionX',
            description: '传送位置X',
            type: 'number',
            default:4
        },
        {
            key: 'transmissionY',
            description: '传送位置Y',
            type: 'number',
            default:4
        },
        {
          key:'music',
          description:'音乐地址',
          type:'string',
          default:''  
        }
        
    ]
}




const map1TileMetadata: DataMetadata = {
    filename:"map.json",
    prefix: 'mapTile',
    title: "地图一图片导入",
    propertyMetadatas: [
        {
            key: 'id',
            description: "ID",
            default: "0",
            type: "number"
        },
        {
            key: 'name',
            description: "图片名字",
            type: "string",
            default: "none"
        },
        {
            key: 'image',
            description: "图片",
            type: "image",
            default: ""
        }

        
    
      
    ]
}
const map2TileMetadata: DataMetadata = {
    filename:"map2.json",
    prefix: 'mapTile',
    title: "地图二图片导入",
    propertyMetadatas: [
        {
            key: 'id',
            description: "ID",
            default: "0",
            type: "number"
        },
        {
            key: 'name',
            description: "图片名字",
            type: "string",
            default: "none"
        },
        {
            key: 'image',
            description: "图片",
            type: "image",
            default: ""
        }
        
    
      
    ]
}

const map3TileMetadata: DataMetadata = {
    filename:"map3.json",
    prefix: 'mapTile',
    title: "地图三图片导入",
    propertyMetadatas: [
        {
            key: 'id',
            description: "ID",
            default: "0",
            type: "number"
        },
        {
            key: 'name',
            description: "图片名字",
            type: "string",
            default: "none"
        },
        {
            key: 'image',
            description: "图片",
            type: "image",
            default: ""
        }
        
    
      
    ]
}

const metadatas: DataMetadata[] = [
    missionMetadata,
    npcMetadata,
    itemMetadata,
    SubmitStatementsMatadata,
    AcceptStatementsMatadata,
    monnsterMetadata
    // mapMetadata,
    // mapTileMetadata
]

const mapMetadatas: DataMetadata[] = [
    map1Metadata,
    map2Metadata,
    map3Metadata,
    map1TileMetadata,
    map2TileMetadata,
    map3TileMetadata
    
]







const buttonGroup = document.getElementById("buttonGroup");
if (buttonGroup) {
    for (let metadata of metadatas) {
        const button = document.createElement("button");
        button.innerHTML = metadata.title;
        buttonGroup.appendChild(button);
        button.onclick = () => {
            currentData.setCurrentMetaData(metadata);
            openPreviewPanel(true);
            changeDataInspector(metadata)
            
            
           
        }
    }
}
const map_buttonGroup = document.getElementById("mapButtonGroup");
if (map_buttonGroup) {
    for (let mapMetadata of mapMetadatas) {
        const button = document.createElement("button");
        button.innerHTML = mapMetadata.title;
        map_buttonGroup.appendChild(button);
        button.onclick = () => {
            currentData.setCurrentMetaData(mapMetadata);
            if (mapMetadata.prefix == "map") {
                openMapEditor();
                changeDataInspector(mapMetadata);
            } else {
                openPreviewPanel(true);
                changeDataInspector(mapMetadata);
            }
            
        }
    }
}

export function save() {
    
    currentData.saveAndReload();
}

const button = document.getElementById("openDebugPanelButton") as HTMLButtonElement;
button.onclick = () => {

    openPreviewDebugPanel();
}
openPreviewPanel(false);
